//
//  CStringUtil.cpp
//  NinjaTurtle
//
//  Created by Nguyen Tri Chung on 1/6/15.
//
//

#include "CStringUtil.h"

vector<string> CStringUtil::split(const string &s, char delim) {
    stringstream ss(s);
    string item;
    vector<string> values;
    while (getline(ss, item, delim)) {
        values.push_back(item);
    }
    
    return values;
}

string CStringUtil::getPattern(const string &s) {
    char returnPattern[200];
    sprintf(returnPattern, "%s%c%s", PLIST_GAME_OBJECT_SD, BIND_DELIMITER, s.data());
    return returnPattern;
}

string CStringUtil::readFile(string fileName, bool writeable, bool override, bool encrypt) {
    string path = "";
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    path = FileUtils::sharedFileUtils()->getWritablePath() + fileName;
    FILE* file = fopen(path.c_str(), "rb");
    if (file == NULL || override) {
        ssize_t fileSize = 0;
        unsigned char* pBuffer = FileUtils::getInstance()->getFileData(fileName.c_str(), "rb", &fileSize);
        
        FILE *fpw = fopen(path.c_str(), "wb");
        fwrite(pBuffer, 1, fileSize, fpw);
        fclose(fpw);
    }
    if (file != NULL) {
        fclose(file);
    }
    
#endif
    
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
    if (writeable) {
        path = FileUtils::getInstance()->getWritablePath() + fileName;
    } else {
        path = FileUtils::getInstance()->fullPathForFilename(fileName.c_str());
    }
#endif
    
    FILE *fp = fopen(path.c_str(), "rb");
    char* allStringData = NULL;
    
    if (fp != NULL) {
        fseek(fp, 0, SEEK_END);
        long fileLength = ftell(fp);
        allStringData = (char*)malloc(fileLength * sizeof(char) + 1);
        fseek(fp, SEEK_SET, 0);
        fread(allStringData, fileLength, 1, fp);
        allStringData[fileLength] = '\0';
        fclose(fp);
    }
    
    if (allStringData == NULL) {
        return "";
    }
    
    if (encrypt) {
        string decryptData = CStringUtil::decrypt(allStringData);
        return decryptData;
    }
    return allStringData;
}

void CStringUtil::writeFile(string data, string fileName, bool encrypt) {
    string path = "";
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    path = FileUtils::getInstance()->getWritablePath() + fileName;
#endif
    
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID && CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
    path = FileUtils::sharedFileUtils()->fullPathForFilename(fileName.c_str());
#endif
    
    FILE *fp = fopen(path.c_str(), "wb");
    string dataToWrite = data;
    if (encrypt) {
        dataToWrite = CStringUtil::encrypt(dataToWrite.data());
    }
    
    if (fp != NULL) {
        fwrite(dataToWrite.data(), sizeof(char), dataToWrite.length(), fp);
        fclose(fp);
    }
}

string CStringUtil::encrypt(string toEncrypt) {
    string keyStr = UG_KEY;
    
    string output = toEncrypt;
    for (int i = 0; i < keyStr.length(); i++) {
        char key = keyStr[i];
        for (int j = 0; j < toEncrypt.size(); j++) {
            output[j] = toEncrypt[j] ^ key;
        }
        toEncrypt = output;
    }
    return output;
}

string CStringUtil::decrypt(string toDecript) {
    string keyStr = UG_KEY;
    
    string output = toDecript;
    for (int i = keyStr.length() - 1; i >= 0; i--) {
        char key = keyStr[i];
        for (int j = 0; j < toDecript.size(); j++) {
            output[j] = toDecript[j] ^ key;
        }
        toDecript = output;
    }
    return output;
}

long CStringUtil::getCurrentTimeInSeconds() {
    time_t timer;
    time(&timer);
    return timer;
}